package cn.community.system.web.controller;

import cn.community.system.domain.Permission;
import cn.community.system.domain.Role;
import cn.community.system.query.PageResult;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IPermissionService;
import cn.community.system.service.IRoleService;
import cn.community.system.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"role/list","角色列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        model.addAttribute("page", roleService.query(qo));
        return "role/list";
    }

    @PostMapping("/distributePermissions")
    @ResponseBody
    @RequiresPermissions(value = {"role/distributePermissions","分配权限"},logical = Logical.OR)
    public Object distributePermissions(Long rid,Long[] ids,Long[] allcb){
        // 删除关联
        for (Long all : allcb) {
            roleService.deleteByRidAndPid(rid,all);
        }
        //处理中间表 (增加关联)
        if(ids!= null && ids.length > 0){
            for (Long pid : ids) {
                roleService.insertRelation(rid,pid);
            }
        }
        return JsonResult.success();
    }

    // 重置权限
    @PostMapping("/deletePermissions")
    @ResponseBody
    @RequiresPermissions(value = {"role/deletePermissions","重置权限"},logical = Logical.OR)
    public Object deletePermissions(Long rid){
        //删除关联
        roleService.deleteRelation(rid);
        return JsonResult.success();
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"role/input","权限分配页面"},logical = Logical.OR)
    public String input(Long id, Model model, @ModelAttribute("qo") QueryObject qo){
        System.out.println(id);
        model.addAttribute("role", roleService.get(id));
        // 查询所有权限
        model.addAttribute("page", permissionService.query(qo));
        // 查询自己拥有的权限
        List<Long> myids = new ArrayList<>();
        List<Permission> permissions = permissionService.listMyPermission(id);
        for (Permission permission : permissions) {
            myids.add(permission.getId());
        }
        model.addAttribute("mypermissions",myids.toString());
        System.out.println(myids.toString());
        return "role/inputPermissions"; // /WEB-INF/views/role/input.ftl
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Role role){
        if(role.getId() == null){ // 代表新增
            roleService.save(role);
        }else { // 代表修改
            roleService.update(role);
        }
        return JsonResult.success();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id){
        if (id != null) {
            roleService.delete(id);
        }
        return JsonResult.success();
    }
}
